package com.example.lxh.testinfo;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.LinkedList;

public class PullTofreshGridActivity extends AppCompatActivity {
    private PullToRefreshGridView m_pullfreshGridView;
    private int ItemCount = 10;
    private LinkedList<String> mlistiTems;
    private ArrayAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_tofresh_grid);
        m_pullfreshGridView = findViewById(R.id.pull_refresh_grid);
        initData();
        mAdapter = new ArrayAdapter<>(this, R.layout.pullfrash_gridview_layout, R.id.tv_gridview, mlistiTems);
        m_pullfreshGridView.setAdapter(mAdapter);
        ILoadingLayout startLables= m_pullfreshGridView.getLoadingLayoutProxy();
        startLables.setPullLabel("开始刷新...");
        startLables.setRefreshingLabel("正在刷新...");

        m_pullfreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                new GetData().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                new GetData().execute();
            }
        });
    }

    private class GetData extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            mlistiTems.add(" " + ItemCount++);
            mAdapter.notifyDataSetChanged();
            m_pullfreshGridView.onRefreshComplete();
        }
    }

    private void initData() {
        mlistiTems = new LinkedList<>();
        for (int i = 0; i < ItemCount; i++) {
            mlistiTems.add(" " + i);
        }
    }
}
